package com.ruoyi.common.oss;

import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.comp.work.domain.CompWork;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/oss")
public class OSSController {

    @Autowired
    private OSSUtils ossUtils;



    @GetMapping("/getStsToken")
    @ResponseBody
    public AjaxResult getStsToken() {
        Map<String, String> shareData = new HashMap<>();
        Map<String, String> result = StsService.getStsToken();
        return AjaxResult.success(result);
    }



    // 1. 上传MultipartFile（SpringMVC文件上传）
    @RequestMapping("/upload")
    @ResponseBody
    public OSSFileRecord uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        // 转换为输入流上传
//        OSSFileRecord record =  ossUtils.uploadInputStreamRecord(
//                file.getInputStream(),
//                file.getOriginalFilename(),
//                CannedAccessControlList.PublicRead, // 公开可读
//                null
//        );

        OSSFileRecord record =  ossUtils.uploadInputStreamRecordMultipart(
                file.getInputStream(),
                file.getOriginalFilename(),
                CannedAccessControlList.PublicRead, // 公开可读
                null
        );



        return record;
    }



    @GetMapping("/batchDownload")
    public void batchDownload(HttpServletResponse response,
                              @RequestParam("objectKeysStr") String objectKeysStr,
                              @RequestParam("downloadName") String downloadName) {
//        List<String> objectKeys = Arrays.asList(
//                "docs/file1.pdf",
//                "images/pic1.png",
//                "data/report.xlsx"
//        );
        if (StringUtils.isEmpty(objectKeysStr)) {
            throw new IllegalArgumentException("参数objectKeys不能为空");
        }
        List<String> objectKeys = Arrays.asList(objectKeysStr.split(","));
        try {
            ossUtils.batchDownloadAndZip(objectKeys, downloadName, response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

















    // 2. 上传本地文件
    public String uploadLocalFile() {
        File localFile = new File("/path/to/local/image.jpg");
        return ossUtils.uploadFile(localFile, "image.jpg");
    }

    // 3. 上传字符串（文本）
    public String uploadText() {
        String content = "这是一段测试文本";
        return ossUtils.uploadString(content, "test.txt");
    }

    // 4. 带元数据的上传（如指定Content-Type）
    public String uploadWithMetadata() throws IOException {
        MultipartFile file = null; // 从请求获取
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType("image/png"); // 指定MIME类型
        return ossUtils.uploadInputStream(
                file.getInputStream(),
                file.getOriginalFilename(),
                CannedAccessControlList.Private, // 私有
                metadata
        );
    }





}
